package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import entity.Hero;


/**
 * JDBC link
 * @author yuzeao
 *
 */
public class DBSConn {
	String url = "jdbc:mysql://localhost:3306/turing_space";
	String username = "student";
	String password = "st_123456";
	Connection con;
	PreparedStatement statement=null;
	public void dbsConn(Hero hero) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection(url,username,password);
			String sql="insert into t_person(name,id,level) values(?,?,?)";
			statement=con.prepareStatement(sql);
			statement.setString(1,hero.getName());
			statement.setString(2, hero.getHP());
			statement.setString(3, hero.getAttack());
			statement.executeUpdate();
//			statement.close();
//			con.close();
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch(SQLException e2) {
			e2.printStackTrace();
		}
		finally {
			if(statement!=null) {
				try {
					statement.close();
				}catch(SQLException e)
				{
					
				}
			}
			if(con!=null) {
				try {
					con.close();
				}catch(SQLException e)
				{
					
				}
			}
		}
	}
}
